//
//  PasswordViewController.m
//  YUXiu
//
//  Created by wang on 16/6/29.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "PasswordViewController.h"
#import "GFForgotPwdViewController.h"


@interface PasswordViewController ()<UITextFieldDelegate>{
    NSArray *titleArray;
    NSArray *placeArray;
    UITableView *pwdTable;
    NSMutableDictionary *passwordDic;

    UIButton *commitBtn;
}

@end

@implementation PasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置密码";
    self.view.backgroundColor = kColorHexString(@"#DBDBDB");
    [self initNav];
}
-(void)autoConfigViews{
    passwordDic = @{}.mutableCopy;
    [passwordDic setValue:@"" forKey:@"old"];
    [passwordDic setValue:@"" forKey:@"new"];
    [passwordDic setValue:@"" forKey:@"new2"];

    titleArray = @[@"旧密码",@"新密码",@"重复密码"];
    placeArray = @[@"请输入旧密码",@"请输入新密码",@"请再次输入密码"];
    pwdTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 45*3) style:UITableViewStylePlain];
    pwdTable.dataSource = self;
    pwdTable.delegate = self;
    [self.view addSubview:pwdTable];
    
    commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 45*4, kScreenWidth-60, 40)];
    [commitBtn setBackgroundColor:kGrayCOLOR];
    commitBtn.layer.cornerRadius = 5;
    commitBtn.selected = NO;
    commitBtn.layer.masksToBounds = YES;
    [commitBtn setTitle:@"确认" forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(doChangePassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];

}
-(void)initNav{
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"忘记密码" style:UIBarButtonItemStylePlain target:self action:@selector(editPassword)];
    
    self.navigationItem.rightBarButtonItem = right;
}
#pragma mark - tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return placeArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"password";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    
    UILabel *tLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
    tLabel.text = titleArray[indexPath.row];
    [cell addSubview:tLabel];
    
    UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(90, 10, kScreenWidth-20-90, 25)];
    TF.placeholder = placeArray[indexPath.row];
    TF.delegate = self;
    TF.tag = indexPath.row;
    [cell addSubview:TF];
    TF.secureTextEntry = YES;
    TF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [TF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    if (textField.tag == 0) {
        [passwordDic setValue:textField.text forKey:@"old"];
       
        
    }else if (textField.tag == 1){
        
        [passwordDic setValue:textField.text forKey:@"new"];
        
    }else{
        
        [passwordDic setValue:textField.text forKey:@"new2"];
    }
    return YES;
}

- (void) textFieldDidChange:(UITextField *) textField {
    if (textField.tag == 2 && passwordDic.count>1) {
        commitBtn.selected = YES;
        [commitBtn setBackgroundColor:kOrangeCOLOR];
        
        }else{
            commitBtn.selected = NO;
            [commitBtn setBackgroundColor:kGrayCOLOR];
    }

}
#pragma mark - TouchEvent 
-(void)editPassword{
    //跳转忘记密码
 GFForgotPwdViewController *ForgetVC = [[GFForgotPwdViewController alloc]init];
    [self.navigationController pushViewController:ForgetVC animated:YES];

}
- (void) doChangePassword {
    
    if (passwordDic.count == 3) {
        [self showHUDWithInfo:@"可以提交" AfterDelay:1];
//        NSDictionary *body = @{@"oldPassword":passwordArray[0],@"newPassword":passwordArray[1],@"confirmedPassword":passwordArray[2]};
     //发请求修改密码
//        [networking requestDataByPost:[NSString stringWithFormat:@"%@/users/modifyPassword",networking.urlStr] header:@{@"Authorization":_user.accesstoken,@"content-type" :@"application/json"} body:body successCallback:^(id responseObject) {
//            NSLog(@"%@",responseObject);
//            
//            [self showHudInView:self.view hint:@"正在修改"];
//            [self.navigationController popToRootViewControllerAnimated:YES];
//            
//        } errorCallback:^(NSError *error) {
//            NSLog(@"%@",error);
//            
//        }];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
